import {Component, EventEmitter, OnInit, Output, ViewChild} from '@angular/core';
import {BsModalService} from 'ngx-bootstrap';
import {isNullOrUndefined} from 'util';
import {BaseGoodsCondition} from '../../../../goods-management/goo-base-goods/base-goods.type';
import {GooBaseGoodsSpecificationCondition} from '../../../../goods-management/goo-base-goods-specification/goo-base-goods-specification.type';
import {Zero3Alert} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Services} from '../../../../../frame/module/services';

@Component({
    selector: 'app-wms-select-goods',
    templateUrl: './wms-select-goods.component.html',
    styleUrls: ['./wms-select-goods.component.css']
})
export class WmsSelectGoodsComponent implements OnInit {


    utils = {
        buildGoodsData: ( goodsItem, goodsSpecificationItem  ) => {
            return {
                goodsUUID: goodsItem.uuid, // 商品ID
                goodsName: goodsItem.serGoodsName, // 商品名称
                goodsSerNum: goodsItem.serNum, // 商品编号
                goodsSpecificationUUID: goodsSpecificationItem.id, // 商品规格ID
                goodsSpecificationName: goodsSpecificationItem.name, // 规格名称
            };
        }
    };
    // 商品查询条件
    condition = new BaseGoodsCondition();
    // 商品规格查询条件
    guiGeCondition = new GooBaseGoodsSpecificationCondition();


    // 表单数据
    formGuiGe = [];
    searchFlag = false;
    selectedGuiGe;
    // 产品规格
    specTypes = [];
    // 商品列表数据
    goodsData;
    // 选择的商品
    selectedGoods;

    constructor(private modalService: BsModalService) {
    }

    @ViewChild('lgModal')
    lgModal;
    @Output()
    saveOrder = new EventEmitter<any>();

    ngOnInit() {
    }

    // 搜索商品
    searchGoods() {
        Services.baseGoods.list(this.condition, {
            page: 0,
            size: 100,
            orders: 'createTime-desc,serNum-asc'
        }).subscribe(data => {
            this.goodsData = data;
            this.searchFlag = true;
        });
    }

    // 选择商品
    doSelectItem(data) {
        this.selectedGoods = data;
        // 查询商品对应的规格
        this.guiGeCondition.fkGooBaseGoodsUuid = data.uuid;
        Services.gooBaseGoodsSpecification.findByFkGooBaseGoodsUuid(this.guiGeCondition).subscribe(item => {
            if (!isNullOrUndefined(item) && item.code === 'success') {
                this.specTypes = [];
                item.domains.forEach(gooBaseGoodsSpecification => {
                    this.specTypes.push(
                        {id: gooBaseGoodsSpecification.uuid, name: gooBaseGoodsSpecification.serName}
                    );
                });
            }
        });
        this.selectedGuiGe = null;
        this.specTypes = [];
        this.formGuiGe = [];
    }

    // 保存
    doSave() {
        if (!this.selectedGoods) {
            return Zero3Alert.req('请选择商品');
        }
        if (!this.selectedGuiGe) {
            return Zero3Alert.req('请选择商品规格');
        }
        this.saveOrder.emit(this.utils.buildGoodsData(this.selectedGoods, this.selectedGuiGe));
        this.lgModal.hide();
        this.clearForm();
    }

    dealGuiGeChange(item) {
        this.selectedGuiGe = item;
    }

    // 清除表单数据
    clearForm() {
        this.selectedGuiGe = null;
        this.specTypes = [];
        this.goodsData = null;
        this.selectedGoods = null;
        this.formGuiGe = [];
    }

    closeMod() {
        this.clearForm();
        this.lgModal.hide();
    }


}